﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Hotel.QLPhong
{
    public partial class OptionPhong : Form
    {
        public string maphong = "" ;
        public OptionPhong(string phong)
        {
            InitializeComponent();
            maphong = phong;
        }
        
        private SqlConnection con;
        private SqlDataAdapter da = new SqlDataAdapter();

        private void connect()
        {
            string cn = "Data Source=.;Initial Catalog=Hotel;Integrated Security=True";

            try
            {
                con = new SqlConnection(cn);
                con.Open();
            }
            catch (Exception)
            {
                MessageBox.Show("Khong the ket noi co so du lieu", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void OptionPhong_Load(object sender, EventArgs e)
        {
            connect();
            SqlDataReader rdr = null;
            try
            {
                SqlCommand cmd = new SqlCommand("select * from Phong where MaPhong = '"+maphong+"'", con);
                rdr = cmd.ExecuteReader();
                while (rdr.Read())
                {
                    tbsophong.Text = rdr[0].ToString();
                    tbloaiphong.Text = rdr[1].ToString();
                    tbsogiuong.Text = rdr[2].ToString();
                    tbsucchua.Text = rdr[3].ToString();
                    tbmota.Text = rdr[4].ToString();
                    tbgia.Text = rdr[5].ToString();
                    tbtiennghi.Text = rdr[7].ToString();
                    if (rdr[6].ToString().Substring(0, 1) == "2")
                    {
                        label7.Text = "Phòng đang được thu dọn";
                        rdb1.Text = "Phòng trống";
                        rdb2.Text = "Thuê phòng";
                        rdb2.Enabled = false;
                    }
                    else
                    {
                        label7.Text = "Phòng trống";
                        rdb1.Text = "Thuê phòng";
                        rdb2.Text = "Phòng đang thu dọn";
                    }
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Lỗi");
            }
        }

        private void btnthoat_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }
        ThongTinKhachHang frmttkh = null;
        private void btnok_Click(object sender, EventArgs e)
        {
            if (rdb1.Text=="Phòng trống" && rdb1.Checked)
            {
                serverdata("UPDATE Phong SET TinhTrang ='0' WHERE MaPhong = '" + tbsophong.Text + "'");
            }
            if (rdb1.Text == "Thuê phòng" && rdb1.Checked)
            {
                frmttkh = new ThongTinKhachHang(tbsophong.Text);
                frmttkh.ShowDialog(this);
                serverdata("UPDATE Phong SET TinhTrang ='1' WHERE MaPhong = '" + tbsophong.Text + "'");

            }
            if (rdb2.Text == "Phòng đang thu dọn" && rdb2.Checked)
            {
                serverdata("UPDATE Phong SET TinhTrang ='2' WHERE MaPhong = '" + tbsophong.Text + "'");
            }
            if (rdb2.Text == "Thuê phòng" && rdb2.Checked)
            {
                frmttkh = new ThongTinKhachHang(tbsophong.Text);
                frmttkh.ShowDialog(this);
                serverdata("UPDATE Phong SET TinhTrang ='1' WHERE MaPhong = '" + tbsophong.Text + "'");
            }
            if (rdbsuaphong.Checked)
            {
                serverdata("UPDATE Phong SET LoaiPhong=N'" + tbloaiphong.Text.Trim() + "', SoGiuong='" + tbsogiuong.Text.Trim() + "', SucChua='" + tbsucchua.Text.Trim() +
                    "', MoTa=N'" + tbmota.Text.Trim() + "', Gia='" + tbgia.Text.Trim() + "', TienNghi=N'" + tbtiennghi.Text.Trim() + "' WHERE MaPhong = '" + tbsophong.Text + "'");
            }
            if (rdbxoaphong.Checked)
            {
                serverdata("UPDATE Phong SET DaXoa = '1' WHERE MaPhong = '" + tbsophong.Text + "'");
            } 
            this.Dispose();
        }

        public void serverdata(string sql)
        {
            try
            {
                connect();
                SqlCommand command = new SqlCommand();
                command.Connection = con;
                command.CommandText = sql;
                command.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                MessageBox.Show("Lỗi");
            }
        }

        private void rdbsuaphong_CheckedChanged(object sender, EventArgs e)
        {
            tbgia.Enabled = true;
            tbloaiphong.Enabled = true;
            tbmota.Enabled = true;
            tbsogiuong.Enabled = true;
            tbsucchua.Enabled = true;
            tbtiennghi.Enabled = true;
        }

        private void rdb1_CheckedChanged(object sender, EventArgs e)
        {
            tbgia.Enabled = false;
            tbloaiphong.Enabled = false;
            tbmota.Enabled = false;
            tbsogiuong.Enabled = false;
            tbsucchua.Enabled = false;
            tbtiennghi.Enabled = false;
        }

        private void rdb2_CheckedChanged(object sender, EventArgs e)
        {
            tbgia.Enabled = false;
            tbloaiphong.Enabled = false;
            tbmota.Enabled = false;
            tbsogiuong.Enabled = false;
            tbsucchua.Enabled = false;
            tbtiennghi.Enabled = false;
        }

        private void rdbxoaphong_CheckedChanged(object sender, EventArgs e)
        {
            tbgia.Enabled = false;
            tbloaiphong.Enabled = false;
            tbmota.Enabled = false;
            tbsogiuong.Enabled = false;
            tbsucchua.Enabled = false;
            tbtiennghi.Enabled = false;
        }
    }
}
